<!DOCTYPE html>
<html>    
  <head>    
    <script src="../../src/svg.js" 
            data-path="../../src/" 
            data-debug="false"></script>
    <script>
      alert('NOTE: This demo does not work yet');
      
      /*
        Standard way to grab XMLHttpRequest object.
      */
      function xhrObj() {
        // Internet Explorer's list of standard XHR PROGIDS
        var XHR_PROGIDS = [
          'MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP',
          'Microsoft.XMLHTTP'
        ];
        
        if (typeof XMLHttpRequest != 'undefined') {
          return new XMLHttpRequest();
        } else if (ActiveXObject) {
          var xhr = null;
          var i; // save the good PROGID for quicker access next time
          for (i = 0; i < XHR_PROGIDS.length && !xhr; ++i) {
            try {
              xhr = new ActiveXObject(XHR_PROGIDS[i]);
            } catch(e) {}
          }

          if (!xhr) {
            throw new Error('XMLHttpRequest object not available on this '
                            + 'platform');
          }

          return xhr;
        }
      }
      
      function parseXML(xml) {
        var xmlDoc;
        if (typeof DOMParser != 'undefined') { // non-IE browsers
          // parse the SVG using an XML parser
          var parser = new DOMParser();
          try { 
            xmlDoc = parser.parseFromString(xml, 'application/xml');
          } catch (e) {
            throw e;
          }

          var root = xmlDoc.documentElement;
          if (root.nodeName == 'parsererror') {
            throw new Error('There is a bug in your SVG: '
                            + (new XMLSerializer().serializeToString(root)));
          }
        } else { // IE
          // only use the following two MSXML parsers:
          // http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
          var versions = [ 'Msxml2.DOMDocument.6.0', 'Msxml2.DOMDocument.3.0' ];

          var xmlDoc;
          for (var i = 0; i < versions.length; i++) {
            try {
              xmlDoc = new ActiveXObject(versions[i]);
              if (xmlDoc) {
                break;
              }
            } catch (e) {}
          }

          if (!xmlDoc) {
            throw new Error('Unable to instantiate XML parser');
          }

          try {
            xmlDoc.preserveWhiteSpace = false;
            // IE will attempt to resolve external DTDs (i.e. the SVG DTD) unless 
            // we add the following two flags
            xmlDoc.resolveExternals = false;
            xmlDoc.validateOnParse = false;
            // Important for MSXML 6
            xmlDoc.setProperty('ProhibitDTD', false);
            xmlDoc.async = 'false';

            var successful = xmlDoc.loadXML(xml);

            if (!successful || xmlDoc.parseError.errorCode !== 0) {
              throw new Error(xmlDoc.parseError.reason);
            }
          } catch (e) {
            console.log(e.message);
            throw new Error('Unable to parse SVG: ' + e.message);
          }
        }

        return xmlDoc;
      }
      
      function handleSVG(svgStr) {
        // parse the XML
        var xml = parseXML(svgStr);
        
        // make a dynamic SVG root
        svg = document.createElementNS(svgns, 'svg');
        svg.setAttribute('width', 640);
        svg.setAttribute('height', 480);
        svg.addEventListener('SVGLoad', function() {
          // get a reference to our SVG root
          svg = this;
          
          // grab the main container group inside our tiger file
          group = xml.documentElement.childNodes[1];
          
          // import it
          group = document.importNode(group, true);
          
          // append it
          svg.appendChild(group);
        }, false);
        // append our new SVG root element to the page
        if (window.svgweb) {
          window.svgweb.appendChild(svg, document.body);
        } else {
          document.body.appendChild(svg);
        }
      }
      
      window.onsvgload = function() {
        var req = xhrObj();
        var url = '../svg-files/tiger.svg';
        req.onreadystatechange = function() {
          if (req.readyState == 4) {
            if (req.status == 200) {
              handleSVG(req.responseText);
            } else { // error
              console.log('Unable to load XML: ' 
                          + req.status + ': ' + req.statusText);
            }
          }
        };

        req.open('GET', url, true);
        req.send(null);
      }
    </script>
  </head>
  
  <body>
    <h1>This sample shows loading an SVG XML file, parsing it using an XML
        parser, and then importing it into the page.</h1>
  </body>
</html>
